package cap.action;

import cap.bean.Admin;
import cap.bean.User;
import cap.dao.AdminDao;
import cap.dao.UserDao;
import cap.dao.impl.AdminDaoImpl;
import cap.dao.impl.UserDaoImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String method=request.getParameter("method");
        if(method.equals("ajax1")){
            ajax1(request,response);
        }else if(method.equals("user1")){
            user1(request,response);
        }else if(method.equals("ajax2")){
            ajax2(request,response);
        }
    }
    private void ajax2(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String username=request.getParameter("name");
        UserDao userDao=new UserDaoImpl();
        int id=userDao.getIdByuserName(username);
        PrintWriter pw=response.getWriter();
        if(id>0){
            pw.write("用户名已存在");
            response.setStatus(200);
        }else{
            response.setStatus(201);
        }
    }
    private void user1(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String t= request.getParameter("type");
        if(t==""||t==null){
            request.getRequestDispatcher("front/404.jsp").forward(request,response);
        }
        int type=Integer.parseInt(t);
        if(type==0){
            UserDao userDao=new UserDaoImpl();
            User user=userDao.login(username,password);
            if(null!=user){
                if(user.getIsDelete()==0){
                    request.getSession().setAttribute("user", user);
                    request.getSession().setAttribute("nickname","username");
                    response.sendRedirect("user?method=index");
                }else{
                    request.getSession().setAttribute("userIsDeleMsg", "该用户已被禁用，无法登录！");
                    response.sendRedirect("login.jsp");
                }
            }else{
                request.getSession().setAttribute("errorMsg", "验证失败，请重新输入用户名或密码！");
                response.sendRedirect("login.jsp");
            }
        }else{
            AdminDao adminDao=new AdminDaoImpl();
            Admin admin=adminDao.login(username,password);
            if(admin!=null){
                request.getSession().setAttribute("admin",admin);
                response.sendRedirect("admin?method=index");
            }else{
                request.getSession().setAttribute("errorMsg","用户名或密码错误");
                response.sendRedirect("login.jsp");
            }
        }
    }
    private void ajax1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String name=request.getParameter("name");
        PrintWriter pw=response.getWriter();
        String randomString=(String)request.getSession().getAttribute("randomString");
        if(randomString.equalsIgnoreCase(name)){
            pw.write("验证码正确！");
            response.setStatus(200);
        }else{
            response.setStatus(201);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
